/**
 * 题目：实现一个type类型，用于约束特殊时间格式的字符串
 * 例子：
 *   FormatDate<"DD-MM-YY">
 *   允许的字符串为：
 *   const date: FormatDate<"DD-MM-YY"> = "12-12-2024" | "12-02-2024";
 *   不允许的字符串为：
 *   const date: FormatDate<"DD-MM-YY"> = "112-12-2024" | "12-112-2024" | "12-12-12024" | ...
 * 时间格式支持多种分隔符："-" | "." | "/"
 * 
 * 当前问题：分隔符不一样也能通过检测
 * 
 */
type Seperator = '-' | '.' | '/'

type Num = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

type NumWithZero = Num | 0

type YY = `${'19' | '20'}${NumWithZero}${NumWithZero}`;

type MM = `0${Num}` | '10' | '11' | '12'

type DD = `${0}${Num}` | `${1 | 2}${NumWithZero}` | `${3}${0 | 1}`

type ParsePart<S extends string> = S extends 'YY' ? YY : S extends 'MM' ? MM : DD

type FormatDate<Pattern extends string> =
    Pattern extends `${infer Aaa}${Seperator}${infer Bbb}${Seperator}${infer Ccc}` ?
    Pattern extends `${Aaa}${infer Sep}${Bbb}${infer _}${Ccc}` ? `${ParsePart<Aaa>}${Sep}${ParsePart<Bbb>}${Sep}${ParsePart<Ccc>}` : never
    : never

/**
    type Sep = '-' | '.' | '/';

    type NumWithZero = '0' | Num;

    type Num = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';

    type YY = `${'19' | '20'}${NumWithZero}${NumWithZero}`;
    type MM = `${0}${Num}` | '10' | '11' | '12';
    type DD = `${0 | 1 | 2}${NumWithZero}` | '30' | '31';

    type ParsePart<S extends string> = S extends 'YY' ? YY : S extends 'MM' ? MM : DD;
    type A = ParsePart<'DD'>;
    //   ^?

    type PP = 'YY' | 'MM' | 'DD';

    type Permute<S extends Sep, U extends PP = PP, E = U> = E extends U
        ? `${E}${[Exclude<U, E>] extends [never] ? '' : `${S}${Permute<S, Exclude<U, E>>}`}`
        : '';

    type _Constraints<U extends Sep, E = U> = E extends U ? Permute<E> : never;
    type Constraints = _Constraints<Sep>;
    //  ^?

    type FormatDate<Pattern extends Constraints> =
        Pattern extends `${infer First}${Sep}${infer Second}${Sep}${infer Third}`
            ? Pattern extends `${First}${infer Sep1}${Second}${Sep}${Third}`
                ? `${ParsePart<First>}${Sep1}${ParsePart<Second>}${Sep1}${ParsePart<Third>}`
                : never
            : never;
 */

export type { FormatDate }

